<template>
  <div class="not-found-page">
    <div class="stars"></div>
    <div class="astronaut">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="astronaut-svg">
        <path fill="#FFD166" d="M256 0c-70.7 0-128 57.3-128 128s57.3 128 128 128 128-57.3 128-128S326.7 0 256 0z"/>
        <path fill="#EF476F" d="M384 160h-32v32h32v32h-32v32h32v32h-32v32h32v32H128v-32h32v-32h-32v-32h32v-32h-32v-32h32v-32h-32V96h256v64z"/>
        <path fill="#06D6A0" d="M128 384h256v64H128z"/>
        <path fill="#118AB2" d="M160 448h192v32H160z"/>
        <circle cx="192" cy="96" r="16" fill="#073B4C"/>
        <circle cx="320" cy="96" r="16" fill="#073B4C"/>
      </svg>
    </div>
    <div class="content">
      <h1>404</h1>
      <h2>哎呀，页面迷失在太空了</h2>
      <p class="description">
        我们找不到您要访问的页面，它可能已被移除、更名或暂时不可用。
      </p>
      <div class="actions">
        <router-link to="/" class="home-btn" @click="goHome">
          <span>返回地球（首页）</span>
          <svg width="13px" height="10px" viewBox="0 0 13 10">
            <path d="M1,5 L11,5"></path>
            <polyline points="8 1 12 5 8 9"></polyline>
          </svg>
        </router-link>
        <button @click="goBack" class="back-btn" v-if="hasHistory">
          <span>返回上一页</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter, useRoute } from 'vue-router'
import { ref, onMounted } from 'vue'
import { getUserRole } from '@/utils/auth' // 假设你有获取用户角色的工具函数

const router = useRouter()
const route = useRoute()
const hasHistory = ref(false)

// 检查是否有历史记录可返回
onMounted(() => {
  hasHistory.value = window.history.length > 1
})

const goBack = () => {
  if (hasHistory.value) {
    router.go(-1)
  } else {
    // 根据用户角色跳转到对应首页
    const userRole = getUserRole() // 获取用户角色
    const homePath = userRole === 'admin' ? '/employee' : '/home'
    router.push(homePath)
  }
}

const goHome = (e) => {
  // 获取当前用户角色
  const userRole = getUserRole()

  // 确定对应的首页路径
  const homePath = userRole === 'admin' ? '/employee' : '/home'

  // 如果已经在首页，阻止默认行为
  if (route.path === homePath) {
    e.preventDefault()
  } else {
    // 跳转到对应的首页
    router.push(homePath)
    e.preventDefault() // 阻止默认的链接行为
  }
}
</script>

<style scoped>
.not-found-page {
  position: relative;
  height: 100vh;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  text-align: center;
}

.stars {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
      radial-gradient(2px 2px at 20px 30px, #eee, rgba(0,0,0,0)),
      radial-gradient(2px 2px at 40px 70px, #fff, rgba(0,0,0,0)),
      radial-gradient(2px 2px at 90px 40px, #ddd, rgba(0,0,0,0)),
      radial-gradient(2px 2px at 130px 80px, #fff, rgba(0,0,0,0));
  background-size: 200px 200px;
  animation: move-stars 200s linear infinite;
}

@keyframes move-stars {
  from { background-position: 0 0; }
  to { background-position: -10000px 5000px; }
}

.astronaut {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  animation: float 6s ease-in-out infinite;
}

.astronaut-svg {
  width: 200px;
  height: auto;
  filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.3));
}

@keyframes float {
  0% { transform: translateY(-50%) translateX(0px); }
  50% { transform: translateY(-50%) translateX(-20px); }
  100% { transform: translateY(-50%) translateX(0px); }
}

.content {
  position: relative;
  z-index: 1;
  max-width: 600px;
  padding: 0 20px;
}

h1 {
  font-size: 120px;
  margin: 0;
  color: #e94560;
  text-shadow: 0 0 10px rgba(233, 69, 96, 0.5);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

h2 {
  font-size: 32px;
  margin: 20px 0;
  font-weight: 300;
}

.description {
  font-size: 18px;
  line-height: 1.6;
  opacity: 0.8;
  margin-bottom: 40px;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.home-btn, .back-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  border-radius: 50px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
}

.home-btn {
  background: #e94560;
  color: white;
  border: none;
}

.home-btn:hover {
  background: #ff6b81;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(233, 69, 96, 0.4);
}

.home-btn svg {
  margin-left: 10px;
  fill: none;
  stroke: white;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: all 0.3s ease;
}

.home-btn:hover svg {
  transform: translateX(3px);
}

.back-btn {
  background: transparent;
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .astronaut {
    display: none;
  }

  h1 {
    font-size: 80px;
  }

  h2 {
    font-size: 24px;
  }

  .description {
    font-size: 16px;
  }

  .actions {
    flex-direction: column;
    gap: 15px;
  }

  .home-btn, .back-btn {
    width: 100%;
  }
}
</style>